package entities;

import interfaze.SpaceInvadersGamingPanel;

import java.awt.Image;
import java.util.ArrayList;

import javax.swing.ImageIcon;

import logic.Utilities;

@SuppressWarnings("serial")
public class Fighter extends Enemy {
	
	private ImageIcon icon;
	
	/*public Fighter(int x, int y, float xScale, float yScale) {
		super(x, y, y, y);
		Image image = Utilities.loadImage("res/Fighter.png");
		if(xScale != 1 || yScale != 1) {
			image = Utilities.resizeImage(xScale, yScale, "res/Fighter.png");
		}
		icon = new ImageIcon(image);
		this.setIcon(icon);
		this.width = image.getWidth(null);
		this.height = image.getHeight(null);
		this.setBounds(this.x, this.y, this.width, this.height);
	}*/

	/**
	 * creates a new Fighter type Enemy entity given coordinates, speed, scale and its row and panel
	 * @param x
	 * @param y coordinates
	 * @param dx
	 * @param dy speed
	 * @param xScale
	 * @param yScale scale
	 * @param fila the row to which the Fighter belongs, important to make sure they don't get out of the screen
	 * @param p panel to which the fighter belongs, useful to access height and width values 
	 */
	public Fighter(int x, int y, int dx, int dy, float xScale, float yScale,ArrayList<Enemy> fila, SpaceInvadersGamingPanel p) {
		super(x, y, dx, dy,fila, p);
		Image image = Utilities.loadImage("res/Fighter.png");
		if(xScale != 1 || yScale != 1) {
			image = Utilities.resizeImage(xScale, yScale, "res/Fighter.png");
		}
		icon = new ImageIcon(image);
		this.setIcon(icon);
		this.width = image.getWidth(null);
		this.height = image.getHeight(null);
		this.setBounds((int)this.x, (int)this.y, this.width, this.height);
		System.out.println("recibidas coordenadas de generacion: "+x+", "+y);
		lastUpdate=System.currentTimeMillis();
	}
	
	public static int getWidth(float xScale) {
		return Math.round(45 * xScale);
	}
	
	public static int getHeight(float yScale) {
		return Math.round(75 * yScale);
	}
	/**
	 * makes the whole row of fighters invert its horizontal speed
	 * @param left this value is true if the row has touched the left limit of the screen
	 */
	public void rowBounceOff(boolean left){
		for(Enemy e: fila){
			e.dx=-e.dx;
			if(left){
				e.x=e.x+1;
			}else{
				e.x=e.x-1;
			}
	}
}
	/**
	 * 
	 * @param velY vertical speed of the shot to be created
	 * @param prob probability of the enemy shooting
	 * @param uDispEn last time (in milliseconds) an enemy shot
	 * @param les list of enemy shots in which to include the shot
	 * @return returns a boolean value stating whether the fighter has shot or not
	 */
	public boolean dispara(int velY, int prob, long uDispEn, ArrayList<EnemyShot> les) {
		boolean res=false;
		if(isVisible()){
		int cont=0;
		for(int i=0;i<=99;i++){
			int temp= (int) ((0 - 10) * Math.random() + 10);
			if(temp%2==0){
				cont++;
			}
		}
		if(cont>=(100-prob) && (System.currentTimeMillis()-uDispEn)>1000){
			EnemyShot temp= new EnemyShot((int) (this.x+(this.getBounds().getWidth()/2)), (int) (this.y+(this.getBounds().getHeight())),0,velY, p);
			p.add(temp,0);
			les.add(temp);
			res=true;
		}
	}
		return res;
	}
}
